package IMDB;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.Normalizer;
import java.util.regex.Pattern;

/**
 *
 * @author Dennis
 */
public class IMDB_searcher {
    
    public IMDB_searcher () {
    }
    
    public URL getResult (String movieName) {
        String string_url;
        if(movieName != null){
            string_url = getURLFromName(movieName).toString();
        } else {
            return null;
        }
        try {
            URL search_url = new URL(string_url);
            
            HttpURLConnection conn = (HttpURLConnection)search_url.openConnection();
            //yc.addRequestProperty("User-Agent", "Internet Explorer/9");
            conn.setAllowUserInteraction(false);         
            conn.setDoOutput(true);
            conn.addRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                int findListValueInt = inputLine.indexOf("findList");
                if(findListValueInt != -1) {
                    inputLine = in.readLine();
                    return new URL("http://www.imdb.com" + inputLine.substring(inputLine.indexOf("result_text") + 23, inputLine.indexOf("?ref", inputLine.indexOf("result_text") + 23) - 1));
                }    
            }
            
            return null;
            
        } catch (Exception e) {
            System.out.println(e);
            return null;
	}
    }
    
    private String getURLFromName (String movieName) {
        movieName = movieName.substring(0, movieName.lastIndexOf('.')); //Remove file extension
        movieName = movieName.replaceAll("\\s","+"); //Replace whitespaces with "+"
        movieName = movieName.replaceAll("!", "%21"); //Replace "&" with %26
        movieName = movieName.replaceAll("&", "%26"); //Replace "&" with %26
        movieName = movieName.replaceAll("'", "%27"); //Replace "'" with %27
        movieName = movieName.replaceAll("\\(", "%28"); //Replace "(" with %28
        movieName = movieName.replaceAll("\\)", "%29"); //Replace "(" with %29
        movieName = movieName.replaceAll(",", "%2C"); //Replace "," with %2C
        
        
        movieName = movieName.replaceAll("å", "a"); //Replace "å" with a
        movieName = movieName.replaceAll("ä", "a"); //Replace "ä" with a
        movieName = movieName.replaceAll("ö", "o"); //Replace "ö" with o
        movieName = movieName.replaceAll("Å", "A"); //Replace "Å" with A
        movieName = movieName.replaceAll("Ä", "A"); //Replace "Ä" with A
        movieName = movieName.replaceAll("Ö", "O"); //Replace "Ö" with O
        
        
        /*
         * Replace disallowed chars in URL
         
        String nfdNormalizedString = Normalizer.normalize(movieName, Normalizer.Form.NFD); 
        Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
        pattern.matcher(nfdNormalizedString).replaceAll("");
        */

        StringBuilder url = new StringBuilder("http://www.imdb.com/find?q=");
        url.append(movieName);
        url.append("&s=tt");

        return url.toString();
    }
    
}
